package com.gljx.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gljx.util.CommonExport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gljx.util.GetFid;
import com.gljx.util.GetFnumber;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Department;
import com.gljx.web.Entity.FillingCard;
import com.gljx.web.Entity.User;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.FillingCardService;

@Controller
@RequestMapping("/")
public class FillingCardController {
	
	//自动注入
	@Autowired
	private FillingCardService fillingCardService;
	
	//自动注入
	@Autowired
	private DepartmentService departmentService;
	
	//自动注入
	@Autowired
	private CustomLogService customLogService;
	
	/**
	 * 查询加油卡
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryFillingCardAll/look.action")
	public String queryFillingCardAll(HttpServletRequest request, String flag) {
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//根据公司id获取信息
		List<FillingCard> fillingCards = fillingCardService.queryFillingCard(user.getFcompanyid());
		//把数据传到页面
		request.setAttribute("fillingCards", fillingCards);
		request.setAttribute("flag", flag);
		return "/backHome/basicData/FillingCard/fillingCardList";
	}
	
	/**
	 * 根据条件查询
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryFillingCardByFnameL2")
	public String queryFillingCardByFnameL2(HttpServletRequest request,String fnameL2){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		if("".equals(fnameL2)){//如果名称为空就调用查询页面
			queryFillingCardAll(request, null);
		}else{
			fnameL2 = fnameL2.trim();//去空格
			//根据公司id和名称查询
			List<FillingCard> fillingCards = fillingCardService.selectFillingCardByfnameL2(user.getFcompanyid(), fnameL2);
			//把数据传到页面
			request.setAttribute("fillingCards", fillingCards);
			request.setAttribute("fnameL2", fnameL2);
		}
		return "/backHome/basicData/FillingCard/fillingCardList";
	}

	/**
	 * 跳转到添加页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/insertFillingCard")
	public String insertFillingCard(HttpServletRequest request,String type){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //获取上一个编码
        /*String number = fillingCardService.selectFnumber(user.getFcompanyid());*/
	    //根据上一个编码获取编码
        String fnumber = GetFnumber.fnumbers("JYK");
	    //获取组织
	  	Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	  	//把数据传到页面
	  	request.setAttribute("department", department);
	  	request.setAttribute("fnumber", fnumber);
	  	request.setAttribute("type", type);
		return "/backHome/basicData/FillingCard/addFillingCard";
	}
	
	/**
	 * 实现添加
	 * @param request
	 * @param fillingCard 属性自动注入
	 * @return
	 */
	@RequestMapping("/addFillingCard")
	public String addFillingCard(HttpServletRequest request,FillingCard fillingCard){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //利用UUID生成fid
	    fillingCard.setFid(GetFid.UUIDIntoFid());
	    //公司id
	    fillingCard.setCfficompanyid(user.getFcompanyid());
		//控制单元id
	    fillingCard.setFcontrolunitid(user.getFcompanyid());
		//创建人id
	    fillingCard.setFcreatorid(user.getFid());
	    //获取当前时间
	  	String createTime = TimeFormat.timeFormat(new Date());
	  	//当前创建时间
	  	fillingCard.setFcreatetime(createTime);
	  	//实现添加
	  	fillingCardService.addFillingCard(fillingCard);
	  	 //添加日志
	    customLogService.addLog("车辆基础资料", createTime+"对加油卡进行添加操作", null, user.getFid());
	    //重定向到添加方法
	    return "redirect:/DrivingSys/insertFillingCard?type=3";
	}
	
	/**
	 * 根据fid查询
	 * @param request
	 * @param fid 要查询的fid
	 * @return
	 */
	@RequestMapping("/selectFillingCardByFid")
	public String selectFillingCardByFid(HttpServletRequest request,String fid){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //获取组织
	  	Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	  	//查询到的对象
		FillingCard fillingCard = fillingCardService.selectByPrimaryKey(fid);
		//把数据传到页面
		request.setAttribute("department", department);
		request.setAttribute("fillingCard", fillingCard);
		return "/backHome/basicData/FillingCard/updateFillingCard";
	}
	
	/**
	 * 修改加油卡
	 * @param request
	 * @param fillingCard 属性自动注入
	 * @return
	 */
	@RequestMapping("/modifyFillingCard")
	public String modifyFillingCard(HttpServletRequest request,FillingCard fillingCard){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //公司id
	    fillingCard.setCfficompanyid(user.getFcompanyid());
		//控制单元id
	    fillingCard.setFcontrolunitid(user.getFcompanyid());
	    //修改人id
	    fillingCard.setFlastupdateuserid(user.getFid());
	    //获取当前修改时间
	  	String updateTime = TimeFormat.timeFormat(new Date());
	  	//当前修改时间时间
	  	fillingCard.setFlastupdatetime(updateTime);
	  	//修改
	  	fillingCardService.modifyFillingCard(fillingCard);
	  	 //添加日志
	    customLogService.addLog("车辆基础资料", updateTime+"对加油卡进行修改操作", null, user.getFid());
	    //调用查询的方法
		return commonController.skipUrl("/queryFillingCardAll/look.action");
	}
	
	/**
	 * 根据fid删除，有可能是多个
	 * @param request
	 * @param deleteId 要删除的fid
	 * @return
	 */
	@RequestMapping("/deleteFillingCardByFid")
	public String deleteFillingCardByFid(HttpServletRequest request,String deleteId[],String fid){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
		//删除
	    if(fid == null || "".equals(fid)){
	    	for(int i = 0;i<deleteId.length;i++){
				fillingCardService.deleteFillingCardByFiD(deleteId[i]);
			}
	    }else{
	    	fillingCardService.deleteFillingCardByFiD(fid);
	    }
		
		 //添加日志
	    customLogService.addLog("车辆基础资料", TimeFormat.timeFormat(new Date())+"对加油卡进行删除操作", null, user.getFid());
	    //调用查询的方法
		queryFillingCardAll(request, null);
		return "/backHome/basicData/FillingCard/fillingCardList";
	}



	/**
	 * 导出Execl
	 *
	 * @param response
	 */
	@RequestMapping("/importFillingCard")
	public void importTrainType(HttpServletResponse response, HttpServletRequest request, String fnameL2) {
		//获取操作人信息
		User user = (User) request.getSession().getAttribute("user");
		List<Object> data = new ArrayList<>();
		//查询数据
        List<FillingCard> fillingCards = fillingCardService.selectFillingCardByfnameL2(user.getFcompanyid(), fnameL2.trim());
        for (FillingCard fillingCard : fillingCards) {
			data.add(fillingCard.getFnumber());//编号
			data.add(fillingCard.getFnameL2());//名称
			data.add(fillingCard.getFsimplename());//班级简称
			data.add(fillingCard.getFdescriptionL2());//描述
			data.add(fillingCard.getFcreatetime());//创建时间
			data.add(fillingCard.getFlastupdatetime());//最后修改时间
			data.add(fillingCard.getCreateName());//创建人
			data.add(fillingCard.getUpdateName());//修改人
			data.add(fillingCard.getCfamount());//余额
		}

		//头部数据
		String head[] = {"编码", "名称", "简称", "描述", "创建时间", "最后修改时间","创建人", "修改人","余额"};
		//长度
		Integer length[] = {5000, 7000, 5000, 5000, 6200, 6200, 3000, 3000,4000};
		try {
			//导出Excel
			CommonExport.importExcel(response, "加油卡" + TimeFormat.getNowTime2() + ".xls", "加油卡", data, head, length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
